package com.bw.home

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil.compose.AsyncImage
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.home.entity.CollectEntity
import com.bw.home.entity.SimpleVideoEntity
import com.bw.home.intent.HomeIntent
import com.bw.home.viewmodel.HomeViewModel
import com.bw.lib_miv.IUiState
import com.bw.lib_miv.RequestType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch


@Composable
fun ProfilePage(
    entity: SimpleVideoEntity,
    vm: HomeViewModel = hiltViewModel(),
    scope: CoroutineScope = rememberCoroutineScope()
) {

    val list = remember {
        SnapshotStateList<SimpleVideoEntity>()
    }


    val id = SPUtils.getInstance().getInt("${entity.id}", 0)

    var state by remember {
        mutableStateOf(
            if (id == 0) false else true
        )
    }


    var image by remember {
        mutableStateOf(
            if (state) {
                R.drawable.ic_home_detail_description_collect_check
            } else {
                R.drawable.ic_home_detail_description_collect
            }
        )
    }

    var tex by remember {
        mutableStateOf(
            if (state) {
                "已收藏"
            } else {
                "收藏"
            }
        )
    }

    LaunchedEffect(key1 = "", block = {
        scope.launch {
            vm.uiState.collect {
                when (it) {

                    is IUiState.OnSuccess -> {
                        if (it.type == RequestType.RECOMMEND_SIMPLE_VIDEO) {
                            list.clear()
                            list.addAll(it.data as Collection<SimpleVideoEntity>)
                        } else if (it.type == RequestType.COLLECT) {
                            ToastUtils.showLong("收藏成功")
                            image = R.drawable.ic_home_detail_description_collect_check
                            state = true
                        } else if (it.type == RequestType.UN_COLLECT) {
                            ToastUtils.showLong("取消收藏")
                            image = R.drawable.ic_home_detail_description_collect
                            state = false
                        }
                    }

                    is IUiState.OnFailed -> {
                        Log.d("TagA", "猜您喜欢)数据获取失败: ${it.msg}")
                    }

                    else -> {}
                }
            }
        }
        vm.sendIntent(HomeIntent.getRecommendSimpleVideo(1, 14))
    })



    Column(
        modifier = Modifier
            .fillMaxWidth()
            .fillMaxHeight()
            .padding(5.dp)
    ) {

        Row(
            Modifier
                .fillMaxWidth()
                .height(50.dp), verticalAlignment = Alignment.CenterVertically
        ) {
            AsyncImage(model = entity.avatar_url, contentDescription = "", Modifier.size(30.dp))

            Text(text = entity.name, fontSize = 18.sp)

        }
        Text(text = entity.title, fontSize = (16.sp))

        Row(
            Modifier
                .fillMaxWidth()
                .height(100.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {

            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_home_detail_description_agree),
                    contentDescription = ""
                )
                Text(text = "9841", fontSize = 16.sp)
            }


            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight()
                    .clickable {
                        if (state) {
                            ToastUtils.showLong("取消收藏")
                            SPUtils
                                .getInstance()
                                .put("${entity.id}", 0)
                            image = R.drawable.ic_home_detail_description_collect
                            tex="收藏"
                        } else {
                            ToastUtils.showLong("收藏成功")
                            SPUtils
                                .getInstance()
                                .put("${entity.id}", entity.id)
                            image = R.drawable.ic_home_detail_description_collect_check
                            tex="已收藏"
                        }
                        state = !state
                    },
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = image),
                    contentDescription = ""
                )
                Text(text = tex, fontSize = 16.sp)
            }


            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_home_detail_description_shared),
                    contentDescription = ""
                )
                Text(text = "262", fontSize = 16.sp)
            }



            Column(
                Modifier
                    .weight(1f)
                    .fillMaxHeight(),
                verticalArrangement = Arrangement.Center,
                horizontalAlignment = Alignment.CenterHorizontally
            ) {
                Image(
                    painter = painterResource(id = R.drawable.ic_home_detail_description_admire),
                    contentDescription = ""
                )
                Text(text = "654", fontSize = 16.sp)
            }
        }

        Text(text = "猜您喜欢")

        LazyVerticalGrid(columns = GridCells.Fixed(2), content = {
            items(list) {
                guessLike(entity = it)
            }
        })

    }
}

@Composable
fun guessLike(entity: SimpleVideoEntity) {

    Column(
        Modifier
            .fillMaxWidth()
            .padding(5.dp)
    ) {

        AsyncImage(
            model = entity.videomainimag, contentDescription = "", contentScale = ContentScale.Crop,
            modifier = Modifier
                .fillMaxWidth()
                .height(150.dp)
        )

        Text(text = entity.title, fontSize = 16.sp)
    }

}